package main

import (
	"flag"
	"fmt"
	"log"
	"os"
	"project-info/src/model"
	"project-info/src/router"
	"project-info/src/version"

	"github.com/gin-gonic/gin"
)

var (
	h   bool
	s   bool
	p   string
	m   bool
	b   string
	x   bool
	c   string
	k   bool
	f   bool
	ver bool
)

func usage() {
	_, err := fmt.Fprintf(os.Stderr, `version: project-info/0.0.1 author: zhangjia
Usage: project-info [-p port] [-h help] [-c config file] 

Options:
 -p  端口
 -h  帮助
 -c  配置文件路径
 -version  构建信息
`)
	if err != nil {
		panic(err.Error())
	}
}

func init() {
	flag.StringVar(&p, "p", "8080", "Yearning端口")
	flag.BoolVar(&h, "h", false, "帮助")
	flag.StringVar(&c, "c", "conf.toml", "配置文件路径")
	flag.BoolVar(&ver, "version", false, "构建信息")
	flag.Usage = usage
	log.SetPrefix("project-info: ")
	log.SetFlags(log.Ldate | log.Lmicroseconds | log.Llongfile)
}

func main() {
	if h {
		flag.Usage()
	} else if ver {
		version.PrintVersion(version.GetVersion())
	} else {
		SetUp()
	}
}

func SetUp() *gin.Engine {
	model.ConfigInit(c)
	model.DbInit()

	// 设置gin模式
	gin.SetMode(gin.ReleaseMode)
	e := gin.New()

	// 添加基础中间件
	e.Use(gin.Logger())
	e.Use(gin.Recovery())

	router.AddRouter(e)
	fmt.Printf("starting: :%s\n", p)
	e.Run(fmt.Sprintf(":%s", p))
	return e
}
